<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<fmt:setBundle basename="messages"/>
<script type="text/javascript" src="js/calendar/PopupWindow.js"></script>
<script type="text/javascript" src="js/calendar/CalendarPopup.js"></script>
<script type="text/javascript" src="js/calendar/AnchorPosition.js"></script>
<script type="text/javascript" src="js/calendar/date.js"></script>
<script type="text/javascript">
	var calFechaNacimiento = new CalendarPopup("div_fechaNacimiento");
    calFechaNacimiento.showNavigationDropdowns();
   	calFechaNacimiento.setCssPrefix("TEST");
    calFechaNacimiento.setYearSelectStartOffset(80);
  		function openCalendar(dateField){
		calFechaNacimiento.select(dateField,'img_purchasedate','dd/MM/yyyy'); 
		return false;
  		}
  		
  		function validateForm(){
  			var remarks      = document.getElementById("remarks");
  			var purchasedate = document.getElementById("purchasedate");
  			var status       = document.getElementById("status"); 
  		  	var errors = "";   			
		
  			if(trim(purchasedate.value) == ''){
  				errors = errors + '<bean:message key="error.message.futurepurchase.wrong.empy"/>\n';
			}else if(!isValidDate(trim(purchasedate.value))){
			    	errors = errors + '<bean:message key="error.message.futurepurchase.wrong.date"/>\n';
			}
  			if( errors == "" ){
  	            return true;
  	        }else{
  	            alert( '<bean:message key="label.common.errors"/> : \n' + errors );
  				return false;
  	        }
  		}
  		function leaveThisForm(forma, purchaseInstructionDetailId){
  			forma.date.value = document.getElementById('purchasedate').value;
  			forma.comments.value = document.getElementById('remarks').value;
  			if( purchaseInstructionDetailId != undefined ){
  				forma.purchaseInstructionDetailId.value = purchaseInstructionDetailId;
  			}
  			forma.submit();
  			return true;
  		}
</script>
<DIV ID="div_fechaNacimiento" STYLE="position:absolute;visibility:hidden;background-color:white;"></DIV>
<html:form action="/goAddInstructionPurchaseDetail">					
	<input type="hidden" name="present" value="1"/>
	<input type="hidden" name="date"/>
	<input type="hidden" name="comments"/>
</html:form>
<html:form action="/editInstructionPurchaseDetail">
	<input type="hidden" name="purchaseInstructionDetailId"/>
	<input type="hidden" name="present" value="1"/>
	<input type="hidden" name="date"/>
	<input type="hidden" name="comments"/>
</html:form>
<html:form action="/deleteInstructionPurchaseDetail">
	<input type="hidden" name="present" value="1"/>
	<input type="hidden" name="purchaseInstructionDetailId"/>
	<input type="hidden" name="date"/>
	<input type="hidden" name="comments"/>
</html:form>
<html:form action="/saveOrUpdateInstructionPurchaseHeader" onsubmit="return validateForm();">
	<table border="0" cellspacing="1"  bgcolor="#CCCCC0" width="500">
		<tr align="left" class="titulocolumna">
			<td class="titulocolumna" align="center" colspan="4"><bean:message key="label.purchases.instructions"/> </td>
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.purchases.instructions.reference"/>:</td>
			<td class="etiqueta_campo" ></td>
			<td class="etiqueta"><bean:message key="label.common.comments"/>:</td>
			<td rowspan="3" valign="top" class="etiqueta_campo"><html:textarea onfocus="this.select();" styleId="remarks" property="view.comments" rows="3" cols="40" styleClass="campo_texto" /> </td>
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="message.futurepurchase.purchase.date.single"/></td>
			<td class="etiqueta_campo">
				<html:text property="view.date" styleClass="campo_texto" styleId="purchasedate" onfocus="this.select();" style="width:70px;" size="10px;" />			 	
				<a href="javascript:var obj_dummy = openCalendar(document.getElementById('purchasedate'));"
					class="pointerhand" NAME="img_purchasedate" ID="img_purchasedate">
					<img id="fechatrigger" src="images/calendar.gif" border="0" />
				</a>
			 </td>
			<td  class="etiqueta_campo">&nbsp;</td>		
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.purchaseinstructiondetail.status"/>: </td>
			<td class="etiqueta_campo">
				<html:select styleId="status" property="view.purchaseStatus" styleClass="campo_texto">
					<html:option value="1"> <bean:message key="label.purchaseinstructiondetail.status.1"/> </html:option>
				</html:select> 
			</td>
			<td class="etiqueta_campo"></td>	
		</tr>
	</table>
	<br/>
	<table width="98%" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
		<tr align="left" class="titulocolumna">
			<td class="titulocolumna" align="center" colspan="13"><bean:message key="label.purchaseinstructiondetail.list"/> </td>
		</tr>
		<tr>
			<td class="titulocolumna" align="center" width="2%">
				<a href="#>" onclick="return leaveThisForm(InstructionPurchaseDetailForm);">
					<img alt="<bean:message key="common.add"/> <bean:message key="label.purchaseinstructiondetail.list"/>" src="images/nuevo.gif" border="0">
				</a>
			</td>
			<td class="titulocolumna" align="center" width="3%">
				<fmt:message key="label.purchaseinstructiondetail.line"/>
			</td>	
			<td class="titulocolumna" align="center" width="25%">
				<fmt:message key="label.purchaseinstructiondetail.customer"/>
			</td>	
			<td class="titulocolumna" align="center" width="24%">
				<fmt:message key="label.purchaseinstructiondetail.products"/>
			</td>	
			<td class="titulocolumna" align="center" width="4%">
				<fmt:message key="label.purchaseinstructiondetail.quantity"/>
			</td>	
			<td class="titulocolumna" align="center" width="6%">
				<fmt:message key="label.purchaseinstructiondetail.cornquantity"/>
			</td>
			<td class="titulocolumna" align="center" colspan="2">
				<fmt:message key="label.purchaseinstructiondetail.adquired.futures"/>
			</td>
			<td class="titulocolumna" align="center" width="6%">
				<fmt:message key="label.purchaseinstructiondetail.type"/>
			</td>
			<td class="titulocolumna" align="center" width="7%">
				<fmt:message key="label.purchaseinstructiondetail.purchase.strategy"/>
			</td>
			<td class="titulocolumna" align="center" width="10%">
				<fmt:message key="label.purchaseinstructiondetail.type.strategy"/>
			</td>
			<td class="titulocolumna" align="center" width="5%">
				<fmt:message key="label.purchaseinstructiondetail.status"/>
			</td>
			<td class="titulocolumna" align="center" width="2%">
				&nbsp;
			</td>
		</tr>
		<logic:present name="PURCHASE_INSTRUCTION_DETAILS">
			<logic:notEmpty name="PURCHASE_INSTRUCTION_DETAILS">
				<c:forEach items="${PURCHASE_INSTRUCTION_DETAILS}" var="detail" varStatus="loop">
					<tr class='
					<c:choose>
					  <c:when test="${loop.index%2 == 0}"><c:out value="columnadatoPar" />
					  </c:when>
					<c:otherwise>
					  <c:out value="columnadatoNon" />
					</c:otherwise>
					</c:choose>'>
						<td valign="top">
							<a href="#>" onclick="return leaveThisForm(InstructionPurchaseDetailEditForm,<c:out value="${loop.index}" />);"><img alt="<bean:message key="common.edit"/> <bean:message key="label.purchaseinstructiondetail.list"/>" src="images/modificar.gif" border="0"></a>
						</td>
						<td align="center" valign="top">
							<fmt:formatNumber type="number" maxIntegerDigits="3" minIntegerDigits="3" value="${loop.index + 1}" pattern="###"/>
						</td>
						<td align="left" valign="top">
							<c:out value="${detail.customerView.customerId}" /> - <c:out value="${detail.customerView.shortDescription}" />
						</td>
						<td align="left">
							<table width="100%" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
								<c:forEach items="${detail.productList}" var="productos" varStatus="loopInside">
									<tr class='<c:choose><c:when test="${loopInside.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>'>
										<td align="center">
											<c:out value="${productos.productId}"></c:out>
										</td>
										<td align="left">
											<c:out value="${productos.shortDescription}"></c:out>
										</td>
									</tr>
								</c:forEach>
							</table>
						</td>
						<td align="right" valign="top">
							<c:out value="${detail.itemQuantity}" />
						</td>			
						<td align="right" valign="top">
							<c:out value="${detail.cornQuantity}" />
						</td>			
						<td  width="2%" align="right" valign="top">
							<c:out value="${detail.futures}" />
						</td>
						<td  width="4%" align="right" valign="top">
							<c:out value="${detail.tons}" />
						</td>
						<td valign="top">
							<fmt:message key="label.futuretype.${detail.futureTypeId}"/>
						</td>
						<td valign="top">
							<fmt:message key="label.strategyid.${detail.instructionStrategyId}"/>
						</td>			
						<td valign="top">
							<fmt:message key="label.strategytypeid.${detail.instructionTypeId}"/>
						</td>
						<td valign="top">
							<fmt:message key="label.purchaseinstructiondetail.status.${detail.status}"/>
						</td valign="top">
						<td align="center" valign="top">
							<a href="#>" onclick="if(confirmDelete('<fmt:formatNumber type="number" maxIntegerDigits="3" minIntegerDigits="3" value="${loop.index + 1}" pattern="###"/>'))location.href='deleteInstructionPurchaseDetail.do?purchaseInstructionDetailId=<c:out value="${loop.index}" />';"><img alt="<bean:message key="common.delete"/> <bean:message key="label.purchaseinstructiondetail.list"/>" src="images/eliminar.jpg" border="0"></a>
						</td>
					</tr>
				</c:forEach>
			</logic:notEmpty>
		</logic:present>
	</table>
	<br/>
	<table border="0" cellspacing="1"  bgcolor="#CCCCC0" width="500">
		<tr>
			<td colspan="4" align="center" class="columnadatoPar">
				<html:submit styleClass="boton_large"  property="sendEmail" ><bean:message key="label.common.save.and.sendMail"/></html:submit>
				<html:submit styleClass="boton" property="dontSendEmail" ><bean:message key="label.common.save"/></html:submit>
				<html:button styleClass="boton" property="backButton" onclick="location.href = 'instructionPurchaseCreateList.do'"><bean:message key="label.common.cancel"/></html:button> 
			</td>
		</tr>
	</table>
</html:form>